package no.ntnu.fp.model;

import no.ntnu.fp.utils.XmlParser;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import java.sql.Date;
import java.sql.Time;

public class Appointment extends Entity {
	protected String place, title, description;
	protected MeetingRoom room;
	protected Employee emp;
	protected Time startTime, endTime;
	protected Date date;
	
	public Appointment(long id, String place, String title, String description,
			MeetingRoom room, Employee emp, Time startTime, Time endTime,
			Date date) {
		super(id);
		this.place = place;
		this.title = title;
		this.description = description;
		this.room = room;
		this.emp = emp;
		this.startTime = startTime;
		this.endTime = endTime;
		this.date = date;
	}

	public Appointment(Element root) {
		super(root);
	}

	@Override
	protected void initializeFromXml(Element rootNode) {
		this.id = Long.parseLong(XmlParser.getTextValue(rootNode, "id"));
		this.title = XmlParser.getTextValue(rootNode, "title");
		this.title = (title == null ? "" : title);
		this.emp = new Employee((Element)rootNode.getElementsByTagName("employee").item(0));
		this.date = Date.valueOf(XmlParser.getTextValue(rootNode, "date"));
		this.startTime = Time.valueOf(XmlParser.getTextValue(rootNode, "startTime"));
		this.endTime = Time.valueOf(XmlParser.getTextValue(rootNode, "endTime"));
		this.description = XmlParser.getTextValue(rootNode, "description");
		this.description = (description == null ? "" : description);
		this.place = XmlParser.getTextValue(rootNode, "place");
		if (this.place == null) {
			Element meetingRoomElement 
				= (Element)rootNode.getElementsByTagName("meetingroom").item(0);

			if (meetingRoomElement.getElementsByTagName("id").getLength() == 1) {
				this.room = new MeetingRoom(meetingRoomElement);
			}
		}
	}

	@Override
	public Element toXml(Document dom) {
		Element rootElement = dom.createElement("appointment");
		rootElement.appendChild(XmlParser.createElementWithValue(dom, "id", id + ""));
		rootElement.appendChild(XmlParser.createElementWithValue(dom, "title", title));
		rootElement.appendChild(emp.toXml(dom));
		rootElement.appendChild(XmlParser.createElementWithValue(dom, "date", date.toString()));
		rootElement.appendChild(XmlParser.createElementWithValue(dom, "startTime", startTime.toString()));
		rootElement.appendChild(XmlParser.createElementWithValue(dom, "endTime", endTime.toString()));
		rootElement.appendChild(XmlParser.createElementWithValue(dom, "description", description));
		Element df = XmlParser.createElementWithValue(dom, "place", place);
		rootElement.appendChild(df);
		if (room != null) {
			rootElement.appendChild(room.toXml(dom));
		} else {
			rootElement.appendChild(XmlParser.createElementWithValue(dom, "meetingroom", "null"));
		}
		
		return rootElement;
	}
	public String getPlace() {
		return place;
	}

	public String getTitle() {
		return title;
	}

	public String getDescription() {
		return description;
	}

	public MeetingRoom getRoom() {
		return room;
	}

	public Employee getEmployee() {
		return emp;
	}

	public Time getStartTime() {
		return startTime;
	}

	public Time getEndTime() {
		return endTime;
	}
	public Date getDate(){
		return date;
	}

	@Override
	public String toString() {
		return super.toString() + " Title: " + getTitle(); 
	}
}
